﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dhgms.Whipstaff.Core.Hosts
{
    using System;

    using Dhgms.Whipstaff.Core.Services;

    using Splat;

    public class FeatureUsageTrackingHost
    {
        private static readonly Lazy<FeatureUsageTrackingHost> LazyInstance = new Lazy<FeatureUsageTrackingHost>(() => new FeatureUsageTrackingHost());

        protected FeatureUsageTrackingHost()
        {
        }

        public static FeatureUsageTrackingHost Instance
        {
            get
            {
                return LazyInstance.Value;
            }
        }

        static internal bool SuppressTracking = false;

        private static readonly Lazy<IFeatureUsageTrackingService> NullTracker = new Lazy<IFeatureUsageTrackingService>(() => new NullFeatureUsageTrackingService());

        public IFeatureUsageTrackingService FeatureUsageTrackingService(Type instanceType)
        {
            if (instanceType == null)
            {
                throw new ArgumentNullException("instanceType");
            }

            if (SuppressTracking) return NullTracker.Value;

            var tracker = Locator.Current.GetService<IFeatureUsageTrackingService>();

            if (tracker == null)
            {
                throw new Exception("IFeatureUsageTrackingService is null. This should never happen, your dependency resolver is broken");
            }

            tracker.Name = instanceType.Name;

            return tracker;
        }
    }
}
